package com.ruoyi.iot.service.impl;

import com.ruoyi.iot.domain.*;
import com.ruoyi.iot.mapper.CollectionDao;
import com.ruoyi.iot.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private CollectionDao collectionDao;
    @Override
    public int deleteCollection(String ID) {
        int deletecount=collectionDao.deleteCollection(ID);
        System.out.println("返回值1==="+deletecount);
        return deletecount;
    }

    @Override
    public boolean updateCollection(CollectPoint collectPoint) {
        int i = collectionDao.updateCollection(collectPoint);
        return  i>0;
    }

    @Override
    public boolean batchDeleteCollection(String[] ids) {
        int i = collectionDao.batchDeleteCollection(ids);
        return  i>0;
    }

    @Override
    public List<CollectPoint> selectCondition(CollectPoint collectPoint) {
        return collectionDao.selectCondition(collectPoint);
    }

    @Override
    public List<CollectPoint> getListGrouping(CollectPoint collectPoint) {
        return collectionDao.getListGrouping(collectPoint);
    }

    @Override
    public int deleteTask(String id) {
        int deletecount=collectionDao.deleteTask(id);
        System.out.println("返回值1==="+deletecount);
        return deletecount;
    }

    @Override
    public boolean batchDeleteTask(String[] ids) {
        int i = collectionDao.batchDeleteTask(ids);
        return  i>0;
    }

    @Override
    public boolean updateTask(CollectTask collectTask) {
        //将当前时间set到实体中
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        collectTask.setUpdatedate(df.format(day));
        int i = collectionDao.updateTask(collectTask);
        return  i>0;
    }

    @Override
	public boolean updatetaskbyid(String taskid) {
		// TODO Auto-generated method stub
    	int count= collectionDao.updatetaskbyid(taskid);
		return count>0;
	}

    @Override
    public boolean saveTask(CollectTask collectTask) {
        //获取随机id
//        collectTask.setId(getGUID());
        //将当前时间set到实体中
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        collectTask.setCreatedate(df.format(day));
        int i = collectionDao.saveTask(collectTask);
        return  i>0;
    }

    @Override
    public List<CollectPoint> getListByTaskId(CollectTask collectTask) {
        return collectionDao.getListByTaskId(collectTask);
    }

    @Override
    public boolean disassociateData(CollectPoint collectPoint) {
        collectPoint.setTaskId(" ");
        return collectionDao.disassociateData(collectPoint);
    }

    @Override
    public List<CollectPoint> getListTime(CollectPoint collectPoint) {
        return collectionDao.getListTime(collectPoint);
    }

    @Override
    public List<CollectPoint> selectConditionAll() {
        return collectionDao.selectConditionAll();
    }

    @Override
    public County dataMatching(String geom) {
        return collectionDao.dataMatching(geom);
    }

    @Override
    public List<CollectPoint> selectaaaaa() {
        return collectionDao.selectaaaaa();
    }

    @Override
    public List<Province> selectGroupAll(CollectPoint collectPoint) {
        List<Province> provinces = collectionDao.selectGroupAll(collectPoint);

        for(Province province :provinces) {
            //通过省查市
            province.setCreateuserId(collectPoint.getCreateuserId());
            if(collectPoint.getOrgId()!=null){
                province.setOrgId(collectPoint.getOrgId());
            }
            List<City> cities =collectionDao.selectGroupByName(province);
            province.setCityList(cities);
            for(City city:cities){
                if(collectPoint.getOrgId()!=null){
                    city.setOrgId(collectPoint.getOrgId());
                }
                city.setCreateuserId(collectPoint.getCreateuserId());
                List<County> counties =collectionDao.selectGroupByCounty(city);
                city.setCounties(counties);
            }

        }
        return provinces;
    }



    @Override
    public boolean saveCollection(CollectPoint collectPoint) {
        int i = collectionDao.saveCollection(collectPoint);
        return  i>0;
    }

    /**
     * 生成16位不重复的随机数，含数字+大小写
     * @return
     */
    public static String getGUID() {
        StringBuilder uid = new StringBuilder();
        //产生16位的强随机数
        Random rd = new SecureRandom();
        for (int i = 0; i < 16; i++) {
            //产生0-2的3位随机数
            int type = rd.nextInt(3);
            switch (type){
                case 0:
                    //0-9的随机数
                    uid.append(rd.nextInt(10));
                    break;
                case 1:
                    //ASCII在65-90之间为大写,获取大写随机
                    uid.append((char)(rd.nextInt(25)+65));
                    break;
                case 2:
                    //ASCII在97-122之间为小写，获取小写随机
                    uid.append((char)(rd.nextInt(25)+97));
                    break;
                default:
                    break;
            }
        }
        return uid.toString();
    }

    /**
     * 批量新增
     * */
	@Override
	public int insertCollectPoints(List<CollectPoint> collectPoints) {
		// TODO Auto-generated method stub
		return collectionDao.insertCollectPoints(collectPoints);
	}

    @Override
    public CollectPoint selectById(String id) {
        return collectionDao.selectById(id);
    }
}
